package com.suiwei.quartz.job;

import com.suiwei.mybatisPlus.service.BookService;
import org.quartz.*;
import org.springframework.context.ApplicationContext;

/**
 * @author suiwei
 * @date 2022-11-06 11:23
 * 定义任务类，实现job
 * Job类无法注入spring容器中的对象,
 * 需要在启动任务的时候，把spring的上线文对象ApplicationContext传递给job的实现类
 */
public class Test implements Job {
    @Override
    /**
     * JobExecutionContext:任务调度的上下文对象，封装正在执行这个任务的相关运行信息
     */
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        ApplicationContext applicationContext = (ApplicationContext) jobDataMap.get("applicationContext");
        //从spring容器中获取对象（IOC容器中获取对象）
        BookService bookService = applicationContext.getBean(BookService.class);
        System.out.println(bookService.list());

    }
}
